package cn.stylefeng.guns.modular.level.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.level.util.ListUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.level.entity.Level;
import cn.stylefeng.guns.modular.level.enums.LevelExceptionEnum;
import cn.stylefeng.guns.modular.level.mapper.LevelMapper;
import cn.stylefeng.guns.modular.level.pojo.request.LevelRequest;
import cn.stylefeng.guns.modular.level.service.LevelService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务实现层
 *
 * @author xbz
 * @date 2023/04/19 10:35
 */
@Service
public class LevelServiceImpl extends ServiceImpl<LevelMapper, Level> implements LevelService {

	@Override
    public void add(LevelRequest levelRequest) {
        Level level = new Level();
        BeanUtil.copyProperties(levelRequest, level);
        this.save(level);
    }

    @Override
    public void del(LevelRequest levelRequest) {
        Level level = this.queryLevel(levelRequest);
        this.removeById(level.getId());
    }

    @Override
    public void edit(LevelRequest levelRequest) {
        Level level = this.queryLevel(levelRequest);
        BeanUtil.copyProperties(levelRequest, level);
        this.updateById(level);
    }

    @Override
    public Level detail(LevelRequest levelRequest) {
        return this.queryLevel(levelRequest);
    }

    @Override
    public PageResult<Level> findPage(LevelRequest levelRequest) {
        LambdaQueryWrapper<Level> wrapper = createWrapper(levelRequest);
        Page<Level> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public PageResult<Level> selectPage(Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<Level> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Level::getFlag,0).orderByDesc(Level::getScore);
        List<Level> levelList = this.baseMapper.selectList(lambdaQueryWrapper);
//        int levelA = (int)(levelList.size()*0.3);
//        Level level1 = levelList.get(levelA-1);
//        Level level2 = levelList.get(levelA);
//        if(level1.getScore() == level2.getScore()){
//            for (Level level : levelList) {
//                if(level.getScore() > level1.getScore() && level.getScore()>= 80){
//                    level.setGrade("A");
//                }else if ( (level.getScore() <= level1.getScore())&&level.getScore()>=80 ){
//                    level.setGrade("B");
//                }else if (level.getScore() < 80){
//                    level.setGrade("C");
//                }
//            }
//        }else{
//            for (Level level : levelList) {
//                if(level.getScore() >= level1.getScore() && level.getScore()>= 80){
//                    level.setGrade("A");
//                }else if ( (level.getScore() < level1.getScore())&&level.getScore()>=80 ){
//                    level.setGrade("B");
//                }else if (level.getScore() < 80){
//                    level.setGrade("C");
//                }
//            }
//        }
        List<Level> list = ListUtil.gradeList(levelList);
        lambdaQueryWrapper.clear();
        lambdaQueryWrapper.eq(Level::getFlag,1);
        List<Level> levelList2 = this.baseMapper.selectList(lambdaQueryWrapper);
        list.addAll(levelList2);


        List<Level> collect = list.stream().sorted(Comparator.comparing(Level::getScore).reversed()).collect(Collectors.toList());

        return PageResultFactory.createPageResult(collect,collect.stream().count(),pageSize,pageNo);
    }

    @Override
    public List<Level> findList(LevelRequest levelRequest) {
        LambdaQueryWrapper<Level> wrapper = this.createWrapper(levelRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author xbz
     * @date 2023/04/19 10:35
     */
    private Level queryLevel(LevelRequest levelRequest) {
        Level level = this.getById(levelRequest.getId());
        if (ObjectUtil.isEmpty(level)) {
            throw new ServiceException(LevelExceptionEnum.LEVEL_NOT_EXISTED);
        }
        return level;
    }

    /**
     * 创建查询wrapper
     *
     * @author xbz
     * @date 2023/04/19 10:35
     */
    private LambdaQueryWrapper<Level> createWrapper(LevelRequest levelRequest) {
        LambdaQueryWrapper<Level> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = levelRequest.getId();
        String name = levelRequest.getName();
        Integer score = levelRequest.getScore();
        String grade = levelRequest.getGrade();
        Integer flag = levelRequest.getFlag();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Level::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(name), Level::getName, name);
        queryWrapper.eq(ObjectUtil.isNotNull(score), Level::getScore, score);
        queryWrapper.like(ObjectUtil.isNotEmpty(grade), Level::getGrade, grade);
        queryWrapper.eq(ObjectUtil.isNotNull(flag), Level::getFlag, flag);

        return queryWrapper;
    }

}